<template>
<!--  技师模块 -->
  <el-form
    ref="formOutRef"
    :model="formOutData"
    :rules="formOutRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
    :disabled="disabled"
  >
    <el-table :data="formOutData" show-summary :summary-method="getSummaries" border class="-mt-10px">
<!--      <el-table-column label="序号" type="index" align="center" width="60" />-->
      <el-table-column label="外协名称" align="center"  min-width="120">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.id`" :rules="formOutRules.id" class="mb-0px!">
            <el-select
              v-model="row.id"
              clearable
              filterable
              placeholder="请选择外协"
            >
              <el-option
                v-for="item in outsourceList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="费用"  prop="cost" align="center"   min-width="120">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.cost`" class="mb-0px!">
            <el-input-number
              v-model="row.cost"
              controls-position="right"
              :min="0.01"
              :precision="0"
              class="!w-100%"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3" v-if="!disabled">
    <el-button @click="handleEngineerAdd" round>+ 添加外协</el-button>
  </el-row>
</template>
<script setup lang="ts">
import {OutsourceVO, OutsourceApi} from '@/api/erp/outsource/index'
import {erpCountInputFormatter, erpPriceInputFormatter, getSumValue} from "@/utils";

const props = defineProps<{
  items: undefined
  disabled: false
}>()
const formLoading = ref(false) // 表单的加载中
const formOutData = ref([])
const formOutRules = reactive({
  productId: [{ required: true, message: '产品不能为空', trigger: 'blur' }],
  count: [{ required: true, message: '产品数量不能为空', trigger: 'blur' }]
})
const formOutRef = ref([]) // 表单 Ref
const outsourceList = ref<OutsourceVO[]>([]) // 外协列表

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
})

/** 初始化设置 */
watch(
  () => props.items,
  async (val) => {
    formOutData.value = val
  },
  { immediate: true }
)


/** 合计 */
const getSummaries = (param) => {
  const { columns, data } = param
  const sums: string[] = []
  columns.forEach((column, index: number) => {
    if (index === 0) {
      sums[index] = '外协费用合计'
      return
    }
    if (['cost'].includes(column.property)) {
      const sum = getSumValue(data.map((item) => Number(item[column.property])))
      sums[index] =
        column.property === 'cost' ? erpCountInputFormatter(sum) : erpPriceInputFormatter(sum)
    } else {
      sums[index] = ''
    }
  })

  return sums
}

/** 新增按钮操作 */
const handleEngineerAdd = () => {

  const engineerRow = {
    id: undefined,
    name: undefined,
    cose: undefined,
    remark: undefined
  }
  if (formOutData.value == null){
    formOutData.value = []
  }
  formOutData.value.push(engineerRow)
}

/** 删除按钮操作 */
const handleDelete = (index: number) => {
  formOutData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formOutRef.value.validate()
}
defineExpose({ validate })

/** 初始化 */
onMounted(async () => {
  const  data = await OutsourceApi.getOutsourcePage(queryParams);
  outsourceList.value = data.list;
  // 默认添加一个
  if (formOutData.value.length === 0) {
    handleEngineerAdd()
  }
})
</script>
